package com.newsmanagement.service.impl;

import com.newsmanagement.common.Constants;
import com.newsmanagement.dao.ISocialDevelopmentServiceContentDao;
import com.newsmanagement.dao.ISocialDevelopmentServiceDao;
import com.newsmanagement.entity.NewsCategoryEntity;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.entity.SocialDevelopmentContentEntity;
import com.newsmanagement.service.ISocialDevelopmentContentService;
import com.newsmanagement.service.ISocialDevelopmentService;
import com.newsmanagement.util.CheckAndChangeUtil;
import com.newsmanagement.util.EntityMapChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xbr
 * 2017/12/4 16:29
 * 社会动态评论管理service实现
 */
@Service
@Transactional
public class SocialDevelopmentContentServiceImpl implements ISocialDevelopmentContentService {


    @Autowired
    private ISocialDevelopmentServiceContentDao socialDevelopmentServiceContentDao;



    /**
     * 发布动态评论（即保存动态评论）
     * @param eumId 操作的用户id
     * @param developmentId 动态id
     * @param contentTxt 评论内容
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages releaseContent(Integer eumId, Integer developmentId, String contentTxt){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id和动态id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(eumId) || !CheckAndChangeUtil.checkIntMoreThanZero(developmentId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"创建人主键或者社会动态主键未找到");
        }

        //验证评论内容
        if(!CheckAndChangeUtil.checkStringHaveData(contentTxt)){
            return returnedMessages.ReturnedMessagesFail(500 ,"请编辑评论内容");
        }

        //添加新闻内容信息
        int addNumber = socialDevelopmentServiceContentDao.releaseContent(eumId ,developmentId,contentTxt);

        if (CheckAndChangeUtil.checkIntMoreThanZero(addNumber)){
            returnedMessages.setData(addNumber);
            return returnedMessages;
        }else{
            return returnedMessages.ReturnedMessagesFail(400 ,"数据插入数据库时出错");
        }
    }

    /**
     * 查询动态下的所有评论
     * @param developmentId 被点赞动态id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages allCommentByDevelopmentIdList(Integer developmentId){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证动态id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(developmentId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"动态主键未找到");
        }

        //根据动态id，查询对应的所有评论
        List<Map<String ,Object>> mapList = socialDevelopmentServiceContentDao.allCommentByDevelopmentIdList(developmentId);

        List list = new ArrayList();
        if (null != mapList){
            for (int i = 0; i < mapList.size(); i++) {
                list.add(EntityMapChange.mapToEntity(mapList.get(i) ,SocialDevelopmentContentEntity.class));
            }
            returnedMessages.setData(list);
            return returnedMessages;
        }else{
            returnedMessages.setCode(400);
            returnedMessages.setMessage("未查找到对应信息");
            return returnedMessages;
        }

    }

}
